import 'package:flutter/material.dart';
import 'package:homepad/iot/basic_node.dart';
import 'package:homepad/iot/basic_types.dart';
import 'package:homepad/iot/air_conditioner.dart';
import 'package:homepad/models/nova_model.dart';
import 'package:homepad/screens/panel_air_conditioner.dart';
import 'package:homepad/styles.dart';
import 'package:homepad/views/animation_icon.dart';
import 'package:homepad/views/item_setting_detail.dart';
import 'package:provider/provider.dart';

class ItemSettingAirConditioner extends StatelessWidget {
  const ItemSettingAirConditioner({super.key});

  @override
  Widget build(BuildContext context) {
    final MyStyles styles = Theme.of(context).extension<MyStyles>()!;
    var air = context.select<NovaModel, AirConditioner?>(
        (nova) => nova.config.getNodeByType(DeviceType.thermostat) as AirConditioner?);
    if (air == null) {
      return const SizedBox();
    }

    var desc = context.select<NovaModel, String>((_) => air.desc);
    BasicNodeState state = context.select<NovaModel, BasicNodeState>((nova) => air.state);

    final iconName = air.icon ?? "icon_font_fan";
    final customIcon = Padding(
        padding: const EdgeInsets.all(3),
        child: Image(
          height: 59,
          width: 59,
          fit: BoxFit.fill,
          image: AssetImage("assets/images/$iconName.png"),
        ));

    return SettingItemDetail(
      iconName,
      title: air.name,
      subtitle: desc,
      customIcon: state == BasicNodeState.activated ? AnimationIcon(child: customIcon) : customIcon,
      onPressed: () {
        showDialog(
            context: context,
            barrierDismissible: true,
            barrierColor: styles.dialogBarrierColor,
            builder: (context) => Dialog(
                  backgroundColor: Colors.transparent,
                  child: PanelAirConditioner(air: air, onCancel: () => Navigator.pop(context)),
                ));
      },
    );
  }
}
